package com.lute.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.security.oauth2.common.json.JSONException;
import org.springframework.security.oauth2.common.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lute.json.SummaryStatusJSON;
import com.lute.model.Client;
import com.lute.model.ClientAccountingPeriod;
import com.lute.model.ClientProfile;
import com.lute.model.EmployeeAccountingPeriod;
import com.lute.model.EmployeeAccountingPeriodState;
import com.lute.model.UserEntitlement;
import com.lute.utils.ServerErrorResponse;

@Controller
public class ClientAdminSummaryScreenServlet {
	
	@RequestMapping (value="/clientAdminSummaryScreen",method= RequestMethod.POST, headers = "Content-type=application/json")
	public @ResponseBody String clientAdminSummaryScreen(@RequestBody String clientJSON, HttpServletRequest request) throws JSONException {
		String result = "";
		String clientId;
		JSONObject jsonReq;
		JSONObject jsonRes = new JSONObject();
		
		HttpSession session = request.getSession(false);
		if(session == null) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_LOGGED_IN.toString());
			result = jsonRes.toString();
			return result;
		}
		
		try {
			jsonReq = new JSONObject(clientJSON);
			clientId = jsonReq.getString("clientId");		
		} catch(JSONException e) {
			jsonRes.put("result", ServerErrorResponse.MALFORMED_REQUEST.toString());
			result = jsonRes.toString();
			return result;
		}
		
		/* check if json data was not malformed */
		if( (jsonReq.isNull("clientId")) ) {
			jsonRes.put("result",ServerErrorResponse.MALFORMED_REQUEST.toString());
			result = jsonRes.toString();
			return result;
		}
		
		String role = (String)session.getAttribute("role");
		
		/* check  authorization */
		if(!(role.equals("clientAdmin"))) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_ENTITLED.toString());
			result = jsonRes.toString();
			return result;
		}
		
		int sessionUserId = (Integer)session.getAttribute("sessionUserId");
		int id_client = Integer.parseInt(clientId);
		
		/* check if clientAdmin has privilege to client */
		ClientProfile clientProfile = UserEntitlement.checkClientAdminPrivilegeToClientProfile(sessionUserId, id_client);
		
		if(clientProfile == null) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_ENTITLED.toString());
			result = jsonRes.toString();
			return result;
		}
		
		try {
			
			ClientAccountingPeriod clientAccountingPeriod = ClientAccountingPeriod.
																getClientCurrentAccountinPeriodWithEmployeeAccPeriods(id_client);
			
			SummaryStatusJSON  clientAdminStatus = new SummaryStatusJSON();
			clientAdminStatus = EmployeeAccountingPeriod.
									prepareSummaryStatus(clientAccountingPeriod.getEmployeeAccountingPeriod(), clientAdminStatus, role);
			
			jsonRes.put("checkedByApproverCount", clientAdminStatus.getCheckedByApproverCount());
			jsonRes.put("checkedByClientAdminCount", clientAdminStatus.getCheckedByClientAdminCount());
			jsonRes.put("initialCount", clientAdminStatus.getInitialCount());
			jsonRes.put("rejectedByApproverCount", clientAdminStatus.getRejectedByApproverCount());
			jsonRes.put("rejectedByClientAdminCount", clientAdminStatus.getRejectedByClientAdminCount());
			jsonRes.put("sentToApproverCount", clientAdminStatus.getSentToApproverCount());
			jsonRes.put("sentToClientAdminCount", clientAdminStatus.getSentToClientAdminCount());
			if(role.equals("clientAdmin")) {
				jsonRes.put("notConfiguredCount", clientAdminStatus.getNotConfigured());
				jsonRes.put("workingForClientCount", clientAdminStatus.getWorkingForClient());
				jsonRes.put("benchedCount", clientAdminStatus.getBenched());
			}
			
		}catch(NullPointerException npe) {
			jsonRes.put("result", ServerErrorResponse.INTERNAL_ERROR.toString());
			result = jsonRes.toString();
			return result;
		}
		
		jsonRes.put("result","OK");
		result = jsonRes.toString();
		return result;
	}
}
